﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web;
using uk.ac.soton.ses.DAL;

namespace uk.ac.soton.ses.WebControls
{
    public class ParameterPositionControl :CompositeControl, IPostBackEventHandler
    {
        private HtmlInputHidden _paramIDToDemoteField = null;
        private HtmlInputHidden _paramIDToInsertField = null;

        public HtmlInputHidden ParamIDToDemoteField
        {
            get
            {
                base.EnsureChildControls();
                return this._paramIDToDemoteField;
            }
        }

        public HtmlInputHidden ParamIDToInsertField
        {
            get
            {
                base.EnsureChildControls();
                return this._paramIDToInsertField;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            
            HyperLink submit = new HyperLink();
            this.Controls.Add(submit);
            submit.ID = "ParameterPositionButton";

            //Postback with the chosen parameters when clicked.
            //Please note that the click() function is called from javascript, not by clicking on it
            submit.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this, "");

            //The CSS class is so we can find the hyperlink and click it from javascript/JQuery
            submit.Attributes["class"] = "ParameterPositionButton";

            this._paramIDToDemoteField = new HtmlInputHidden();
            this.Controls.Add(_paramIDToDemoteField);
            this._paramIDToDemoteField.ID = "ParamIDToDemote";
            this._paramIDToDemoteField.Attributes["class"] = "ParamIDToDemote";

            this._paramIDToInsertField = new HtmlInputHidden();
            this.Controls.Add(_paramIDToInsertField);
            this._paramIDToInsertField.ID = "ParamIDToInsert";
            this._paramIDToInsertField.Attributes["class"] = "ParamIDToInsert";
        }
        
        
        public void RaisePostBackEvent(string eventArgument)
        {
            if (this._paramIDToDemoteField != null && this._paramIDToInsertField != null)
            {
                string parameterIDToDemotestr = this.Page.Request.Form[this._paramIDToDemoteField.UniqueID];
                string parameterIDToInsertstr = this.Page.Request.Form[this._paramIDToInsertField.UniqueID];

                int parameterIDToDemote = -1;
                int parameterIDToInsert = -1;
                if (int.TryParse(parameterIDToInsertstr, out parameterIDToInsert) && int.TryParse(parameterIDToDemotestr, out parameterIDToDemote))
                {
                    MaterialsMetadataContext dbcontext = HttpContext.Current.GetMaterialsDBContext();
                    dbcontext.ParameterReposition(parameterIDToInsert, parameterIDToDemote);
                    dbcontext.SaveChanges();
                }
                this.RaiseBubbleEvent(this, new RebindRequiredEventArgs());
            }
        }
    }
}
